using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FreshCommonUtility.Dapper;
using FreshCommonUtility.SqlHelper;
using WeChatCmsCommon.EnumBusiness;
using WeChatModel.DatabaseModel;

namespace WeChatDataAccess
{
    /// <summary>
    /// 人员标签数据访问
    /// </summary>
    public class PersonTagData : BaseData<long, PersonTagModel>
    {
        /// <summary>
        /// 根据人员ID获取标签列表
        /// </summary>
        /// <param name="personId">人员ID</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public List<PersonTagModel> GetListByPersonId(long personId, long userId)
        {
            var where = new StringBuilder(" WHERE PersonId = @PersonId AND UserId = @UserId AND IsDel = @IsDel AND IsValid = @IsValid ");
            var param = new
            {
                PersonId = personId,
                UserId = userId,
                IsDel = FlagEnum.HadZore.GetHashCode(),
                IsValid = FlagEnum.HadOne.GetHashCode()
            };

            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                return conn.GetList<PersonTagModel>(where.ToString(), param)?.OrderBy(f=>f.Sort).ToList() ?? new List<PersonTagModel>();
            }
        }

        /// <summary>
        /// 保存标签信息
        /// </summary>
        /// <param name="model">标签模型</param>
        /// <returns>标签ID</returns>
        public long SaveModel(PersonTagModel model)
        {
            if (model == null) return 0;
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                if (model.Id > 0)
                {
                    // 更新
                    conn.Update(model);
                    return model.Id;
                }
                else
                {
                    // 添加
                    return conn.Insert<long, PersonTagModel>(model);
                }
            }
        }

        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id">标签ID</param>
        public void DelModel(long id)
        {
            if (id < 1) return;
            var model = Get(id);
            if (model == null) return;
            model.IsDel = FlagEnum.HadOne;
            model.UpdateTime = DateTime.Now;
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                conn.Update(model);
            }
        }

        /// <summary>
        /// 检查标签是否存在
        /// </summary>
        /// <param name="personId">人员ID</param>
        /// <param name="tagName">标签名称</param>
        /// <param name="userId">用户ID</param>
        /// <param name="excludeId">排除的标签ID（编辑时使用）</param>
        /// <returns></returns>
        public bool IsTagExists(long personId, string tagName, long userId, long? excludeId = null)
        {
            var whereStr = new StringBuilder(" WHERE PersonId = @PersonId AND TagName = @TagName AND UserId = @UserId AND IsDel = @IsDel ");
            object param;

            if (excludeId.HasValue)
            {
                whereStr.Append(" AND Id != @ExcludeId ");
                param = new
                {
                    PersonId = personId,
                    TagName = tagName,
                    UserId = userId,
                    IsDel = FlagEnum.HadZore.GetHashCode(),
                    ExcludeId = excludeId.Value
                };
            }
            else
            {
                param = new
                {
                    PersonId = personId,
                    TagName = tagName,
                    UserId = userId,
                    IsDel = FlagEnum.HadZore.GetHashCode()
                };
            }

            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                var count = conn.RecordCount<PersonTagModel>(whereStr.ToString(), param);
                return count > 0;
            }
        }
    }
}